package com.hxexp.weixin.test;

import java.util.HashMap;

import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;

import com.hxexp.weixin.common.math.RandomUtil;
import com.hxexp.weixin.test.runner.JUnit4WithSpringLogClassRunner;
import com.qq.weixin.common.security.WeixinSignatureUtil;
import com.qq.weixin.constant.AppDevConstant;
import com.qq.weixin.mp.api.pay.PayAPI;
import com.qq.weixin.mp.api.pay.model.UnifiedOrderJaxb;
import com.qq.weixin.mp.api.pay.model.UnifiedOrderParamJaxb;

/**
 * 支付接口测试
 * @author li.jiang
 *
 */
@RunWith(JUnit4WithSpringLogClassRunner.class)
@ContextConfiguration(locations={"classpath:conf/spring/spring-biz.xml", "classpath:conf/spring/spring-ehcache.xml"})
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class PayAPITester {
    @Autowired
    private PayAPI payAPI;
    @Test
    public void testCase01_UnifiedOrderAPI() {
        HashMap<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("appid", AppDevConstant.HxExp.APPID);
        paramMap.put("mch_id", AppDevConstant.HxExp.MCHID);
        paramMap.put("nonce_str", RandomUtil.getRandomString(32));
        paramMap.put("body", "测试车票");
        paramMap.put("out_trade_no", "D201505211805301029348T");
        paramMap.put("total_fee", "1");
        paramMap.put("spbill_create_ip", "8.8.8.8");
        paramMap.put("notify_url", "http://weixin.huaxiaexpress.com.cn/weixinticket/paynotify/confirm.do");
        paramMap.put("trade_type", "JSAPI");
        paramMap.put("openid", "o5GuMs3VA_8xB6VCNKaTdFQHVQ_U");

        String paySign = WeixinSignatureUtil.generatePaySign(AppDevConstant.HxExpTest.WXPAY_API_SECRETKEY, paramMap);
        UnifiedOrderParamJaxb paramJaxb = new UnifiedOrderParamJaxb();
        paramJaxb.setAppId(AppDevConstant.HxExp.APPID);
        paramJaxb.setMchId(AppDevConstant.HxExp.MCHID);
        paramJaxb.setNonceStr(paramMap.get("nonce_str"));
        paramJaxb.setBody("测试车票");
        paramJaxb.setOutTradeNo("D201505211805301029348T");
        paramJaxb.setTotalFee(1);
        paramJaxb.setSpbillCreateIp("8.8.8.8");
        paramJaxb.setNotifyUrl("http://weixin.huaxiaexpress.com.cn/weixinticket/paynotify/confirm.do");
        paramJaxb.setTradeType("JSAPI");
        paramJaxb.setOpenId("o5GuMs3VA_8xB6VCNKaTdFQHVQ_U");
        paramJaxb.setSign(paySign);

        try {
            UnifiedOrderJaxb uorderJaxb = payAPI.unifiedOrderAPI(paramJaxb);
            System.out.println(uorderJaxb);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
